﻿using System;
using ProtoBuf;
using System.IO;
using UnityEngine;

public static class Util
{
    /// <summary>
    /// protobuf的序列化（实体类->byte流）
    /// </summary>
    /// <typeparam name="T">protobuf的实体类型</typeparam>
    /// <param name="t">实体对象</param>
    /// <returns></returns>
    public static byte[] Serilize<T>(T t) where T : IExtensible//来自protobuf
    {
        MemoryStream stream = new MemoryStream();
        Serializer.Serialize(stream, t);
        return stream.ToArray();
    }

    /// <summary>
    /// protobuf的反序列化（bytes流->实体类）
    /// </summary>
    /// <typeparam name="T">protobuf的实体类型</typeparam>
    /// <param name="data">数据流</param>
    /// <returns></returns>
    public static T Deserilize<T>(byte[] data) where T: IExtensible//来自protobuf
    {
        MemoryStream stream = new MemoryStream(data);
        T t = Serializer.Deserialize<T>(stream);
        return t;
    }

    public static string UUID
    {
        get{
            return SystemInfo.deviceUniqueIdentifier + UnityEngine.Random.Range(0, 10000);
        }
    }

    public static Actor CreateActor(int sex)
    {
        string path = "Actor/";
        if(sex==0)
            path += "nv";
        else
            path += "nan";

        var prefab = Resources.Load<GameObject>(path);
        var go = GameObject.Instantiate(prefab);
        go.transform.position = new Vector3(0,0,10);
        var actor = go.GetComponent<Actor>();
        if (actor == null)
        {
            actor = go.AddComponent<Actor>();
        }
        return actor;
    }
}
